package com.cy.aircompressor.main.home.other.provider;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentManager;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.cy.aircompressor.R;
import com.cy.aircompressor.databinding.ItemWarningLayoutBinding;
import com.cy.aircompressor.entity.warning.WarningMsg;
import com.cy.aircompressor.main.home.other.WarningDetailFragment;
import com.drug.lib.view.recycptr.ItemViewBinder;
import com.jakewharton.rxbinding2.view.RxView;
import com.net.model.base.BaseBindViewHolder;

import java.text.DecimalFormat;
import java.util.concurrent.TimeUnit;

public class WarningProvider extends ItemViewBinder<WarningMsg, WarningProvider.ViewHolder> {
    Context mContext;
    FragmentManager fragmentManager;

    public WarningProvider(FragmentManager fragmentManager, Context mContext) {
        this.mContext = mContext;
        this.fragmentManager = fragmentManager;
    }

    @NonNull
    @Override
    protected WarningProvider.ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        return new ViewHolder(ItemWarningLayoutBinding.inflate(inflater, parent, false));
    }

    @Override
    protected void onBindViewHolder(@NonNull WarningProvider.ViewHolder holder, @NonNull WarningMsg entity) {
        holder.mBinding.serial.setText(holder.getLayoutPosition() + "");
        holder.mBinding.airName.setText(entity.getCompressorName());
        holder.mBinding.compressorPart.setText(entity.getCompressorPartDesc());
        holder.mBinding.compressorPoint.setText(entity.getCompressorPointDesc());
        DecimalFormat decimalFormat = new DecimalFormat("0.00");
        if (TextUtils.equals(entity.getCompressorPointCode(), "pressure")) {
            holder.mBinding.value.setText(String.format("%s %s", decimalFormat.format(entity.getValue()), "Mpa"));
        } else {
            holder.mBinding.value.setText(String.format("%s %s", decimalFormat.format(entity.getValue()), "℃"));
        }
        holder.mBinding.alarmRules.setText(entity.getAlarmRules());
        holder.mBinding.status.setText(entity.getStatus() == 1 ? "已处理" : "未处理");
        holder.mBinding.handle.setVisibility(entity.getStatus() == 1 ? View.GONE : View.VISIBLE);
        holder.mBinding.status.setTextColor(Color.parseColor(entity.getStatus() == 1 ? "#15CB22" : "#F04538"));
        holder.mBinding.warningDate.setText(entity.getCreateTime());
        RxView.clicks(holder.mBinding.detail)
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(o -> {
                    WarningDetailFragment.showDialogFragment(fragmentManager, "detail", entity);
//                    showDialog(entity);
                });
        RxView.clicks(holder.mBinding.handle)
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(o -> {
                    showHandleDialog(entity);
                });
        holder.itemView.setBackgroundColor(Color.parseColor(holder.getAdapterPosition() % 2 == 0 ? "#1B485E" : "#0E3A50"));
    }

    private void showDialog(WarningMsg warningMsg) {
        new MaterialDialog.Builder(mContext)
                .title("告警详情")
                .content(warningMsg.getAlarmRules())
                .positiveText("确定")
                .canceledOnTouchOutside(false)
                .contentColor(ContextCompat.getColor(mContext, R.color.white))
                .titleColor(ContextCompat.getColor(mContext, R.color.white))
                .backgroundColor(ContextCompat.getColor(mContext, R.color.default_background))
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        dialog.dismiss();
                    }
                })
                .show();
    }

    private void showHandleDialog(WarningMsg warningMsg) {
        new MaterialDialog.Builder(mContext)
                .title("提示")
                .content("确定要将该告警标记为已处理状态吗？")
                .positiveText("确定")
                .negativeText("取消")
                .canceledOnTouchOutside(false)
                .contentColor(ContextCompat.getColor(mContext, R.color.white))
                .titleColor(ContextCompat.getColor(mContext, R.color.white))
                .backgroundColor(ContextCompat.getColor(mContext, R.color.default_background))
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        if (handleCallBack != null)
                            handleCallBack.handle(warningMsg);
                    }
                })
                .onNegative(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        dialog.dismiss();
                    }
                })
                .show();
    }

    HandleCallBack handleCallBack;

    public void setHandleCallBack(HandleCallBack handleCallBack) {
        this.handleCallBack = handleCallBack;
    }

    public interface HandleCallBack {
        void handle(WarningMsg warningMsg);
    }

    public class ViewHolder extends BaseBindViewHolder<ItemWarningLayoutBinding> {

        protected ViewHolder(ItemWarningLayoutBinding itemView) {
            super(itemView);
        }
    }


}
